package com.ddtech.framework.security.service;

import com.ddtech.framework.security.constant.ScanLoginEnum;
import com.ddtech.framework.security.security.ScanLogin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author bin
 */
@Service
public class ScanLogService {
    @Value("${loginCode.expiration}")
    private Long expiration;
    private final RedisTemplate redisTemplate;

    public ScanLogService(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void initScanCode(String key) {
        ScanLogin scanLogin = new ScanLogin(ScanLoginEnum.SCAN_CODE_INIT);
        redisTemplate.opsForValue().set(key, scanLogin);
        redisTemplate.expire(key, expiration, TimeUnit.MINUTES);
    }

    public void updateSanCode(String key, ScanLogin scanLogin) {
        redisTemplate.opsForValue().set(key, scanLogin);
        redisTemplate.expire(key, expiration, TimeUnit.MINUTES);
    }

    public ScanLogin getScanCode(String key) {
        ScanLogin scanLogin = (ScanLogin) redisTemplate.opsForValue().get(key);
        return scanLogin;
    }
}
